﻿Shader "Hidden/GaussianBlur"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_TexelSize;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = fixed4(0,0,0,0);
                // 3x3 高斯核权重
                float weight[3] = {0.25, 0.5, 0.25};

                // 水平模糊
                for (int x = -1; x <= 1; x++)
                {
                    col += tex2D(_MainTex, i.uv + float2(x * _MainTex_TexelSize.x, 0)) * weight[x + 1];
                }

                fixed4 tempCol = col;
                col = fixed4(0,0,0,0);

                // 垂直模糊
                for (int y = -1; y <= 1; y++)
                {
                    col += tex2D(_MainTex, i.uv + float2(0, y * _MainTex_TexelSize.y)) * weight[y + 1];
                }

                return col;
            }
            ENDCG
        }
    }
}    